import { reqSendCode,reqRegister,reqLogin,requserInfo,reqlogout } from '@/api'
import {setToken,getToken,removeToken} from '@/utils/token'
const state = {
    code: '',
    token: getToken(),
    ueserinfo: {}
}
const mutations = {
    SETSENDCODE(state, code) {
        // console.log('11');
        state.code = code
    },
    GETLOGIN(state, token) {
        state.token = token
    },
    GETUSERINFO(state,userinfo) {
        state.ueserinfo = userinfo
    },
    // 清除本地数据
    CLEAR(state) {
        state.token = ''
        state.ueserinfo = {}
        removeToken()
    }
}
const actions = {
    async GetSendCode({ commit }, phone) {
        console.log('phone', phone);
        const result = await reqSendCode(phone)
        if (result.code == 200) {
            commit('SETSENDCODE', result.data)
        } else {
            return Promise.reject(new Error('faile'))
        }
    },
    async GetRejister({ commit }, user) {
        const result = await reqRegister(user)
        if (result.code == 200) {
            console.log('result', result);
        } else {
            return Promise.reject(new Error('faile'))
        }
    },
    //登录业务
    async GetLogin({ commit }, data) {
        try {
            const result = await reqLogin(data)
            commit('GETLOGIN',result.data.token);
            // localStorage.setItem("USERLOGIN",result.data.token)
            setToken(result.data.token);
        } catch (error) {
            throw error
        }
    },
    // 获取用户信息
    async GetuserInfo({commit},data) {
        console.log(data);
        const result =  await requserInfo(data)
        if(result.code == 200) {
            // console.log(result);
            commit('GETUSERINFO',result.data)
        } else {
            // return Promise.reject(new Error('faile'))
        }
    },
    // 退出登录
    async userLogout({commit}) {
        let result = await reqlogout()
        if(result.code = 200) {
            commit("CLEAR");
        }else {

        }
    }
}
const getters = {}
export default {
    state,
    mutations,
    actions,
    getters
}